//
//  HKProfileViewController.m
//  HKTimeKitApp
//
//  Created by huzhaoke1 on 2023/5/8.
//

#import "HKProfileViewController.h"
#import <Masonry/Masonry.h>
#import "HKTableAdapter.h"
#import "HKHeaderCellViewModel.h"
#import "HKHeaderCellTableViewCell.h"
#import "HKCommonDefine.h"
#import "HKUser.h"
#import "HKUserInfoViewController.h"
#import "HKProfileHeaderView.h"
#import "UIView+properties.h"
#import "HKProfileListCellModel.h"
#import <JPUSHService.h>
#import "HKHeartBeatViewController.h"

#define KSContentLeft  20.f

@interface HKProfileViewController () <nameHKHeaderCellTableViewCellDelegate>

@property (nonatomic, strong) HKTableAdapter *tableAdapter;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *settingButton;

@end

@implementation HKProfileViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubViews];
    [self setupModels];
    self.userDefinedNaviBar.hidden = YES;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled =NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { self.navigationController.interactivePopGestureRecognizer.enabled =YES;
    }
}

- (void)setupSubViews {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    [self.view addSubview:self.settingButton];
    CGFloat top =[HKKeyWindow safeAreaInsets].top + 24 + 4;
    [self.settingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(top);
        make.right.mas_equalTo(self.view).offset(-KSContentLeft);
        make.size.mas_equalTo(CGSizeMake(36, 36));
    }];
}

- (void)setupModels {
    HKHeaderCellViewModel *headerModel = [HKHeaderCellViewModel new ];
    headerModel.cellClass = HKHeaderCellTableViewCell.class;
    headerModel.cellHeight = [HKHeaderCellTableViewCell cellHeightWithContent:nil];
    [headerModel setRender:^(__kindof HKListCellModel * _Nonnull cellModel, id  _Nonnull cell) {
        HKHeaderCellTableViewCell *tbCell = KSSAFE_CAST(cell, HKHeaderCellTableViewCell);
        tbCell.delegate = self;
        HKUser *user = WMglobalUser();
        [tbCell updateContent:user];
    }];
    WS
    [headerModel setCellDidSelectBlock:^(HKListCellModel * _Nonnull cellModel, NSIndexPath * _Nonnull indexPath, id  _Nonnull cell) {
      SS
        [self toUserInfoController];
    }];
    HKListSectionModel *sectionModel = [HKListSectionModel new];
    [sectionModel addCellModel:headerModel];
    
    HKListSectionModel *sectionModel1 = [HKListSectionModel new];
    
    HKSectionHeaderFooterViewModel *headerViewModel1 = [self createHeaderViewModelWithImageName:@"" title:@""];
    sectionModel1.headerViewModel = headerViewModel1;
    
    HKProfileListCellModel *cellModel = [HKProfileListCellModel new];
    cellModel.context.fromViewController = self;
    [sectionModel1 addCellModel:cellModel];
    
    [self.tableAdapter addSectionModels:@[sectionModel, sectionModel1]];

    [self.tableAdapter reloadData];
}

- (HKSectionHeaderFooterViewModel *)createHeaderViewModelWithImageName:(NSString *)imageName title:(NSString *)title {
    HKSectionHeaderFooterViewModel *headerViewModel = [HKSectionHeaderFooterViewModel new];
    headerViewModel.height = [HKProfileHeaderView cellHeightWithContent:nil];
    headerViewModel.width = self.view.width;
    headerViewModel.viewClass = HKProfileHeaderView.class;
    return headerViewModel;
}

- (void)onTapSetting:(id)sender {
    HKHeartBeatViewController *controller = [HKHeartBeatViewController new];
    [controller setToGetFrequencyBlock:^(NSInteger frequency) {
        NSLog(@"%d",frequency);
    }];
    [self.navigationController pushViewController:controller animated:YES];
    return;
    UIAlertController *alert = [UIAlertController
                                
                                alertControllerWithTitle:nil message:nil
                                
                                preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消"   style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
        
    }];
    UIAlertAction *logOut = [UIAlertAction actionWithTitle:@"退出登录"   style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        HKUser *user = WMglobalUser();
        if (user) {
            [JPUSHService deleteAlias:^(NSInteger iResCode, NSString * _Nullable iAlias, NSInteger seq) {
                if (iResCode == 6002 || iResCode == 6014 || iResCode == 6020 || iResCode == 6021 || iResCode == 6022) {
                    NSLog(@"清除别名 code：%ld",(long)iResCode);
                } else if(iResCode == 0){
                    NSLog(@"清除别名成功");
                }else{
                    NSLog(@"清除别名 失败code：%ld",(long)iResCode);
                }
            } seq:0];
        }
    }];
//    UIAlertAction *unregisterButton = [UIAlertAction actionWithTitle:@"注销用户"   style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//        logout();
//    }];
    logout();
    [alert addAction:logOut];
    //[alert addAction:unregisterButton];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

#pragma mark - delegate
- (void)headerCellTableViewCell:(HKHeaderCellTableViewCell *)cell
          didTapHeaderImageView:(UIImageView *)headerImageView {
    [self toUserInfoController];
    
}

- (void)toUserInfoController {
    HKUserInfoViewController *userInfoController = [HKUserInfoViewController new];
    [self.navigationController pushViewController:userInfoController animated:YES];
}

#pragma mark - getter and setter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [HKTableAdapter createTableViewWithFrame:self.view.bounds];
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}

- (HKTableAdapter *)tableAdapter {
    if (!_tableAdapter) {
        _tableAdapter = [[HKTableAdapter alloc] initWithTableView:self.tableView];
    }
    return _tableAdapter;
}

- (UIButton *)settingButton {
    if (!_settingButton) {
        _settingButton = [UIButton new];
        _settingButton.hidden = YES;
        [_settingButton setBackgroundImage:[UIImage imageNamed:@"iOS_common_icon_setting"] forState:UIControlStateNormal];
        [_settingButton addTarget:self action:@selector(onTapSetting:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _settingButton;
}

@end
